.TH std::basic_fstream::basic_fstream 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_fstream::basic_fstream \- std::basic_fstream::basic_fstream

.SH Synopsis
   basic_fstream();                                                   \fB(1)\fP
   explicit basic_fstream( const char* filename,

                           std::ios_base::openmode mode               \fB(2)\fP

                               = std::ios_base::in |
   std::ios_base::out );
   explicit basic_fstream( const std::filesystem::path::value_type*
   filename,

                           std::ios_base::openmode mode               \fB(3)\fP \fI(since C++17)\fP

                               = std::ios_base::in |
   std::ios_base::out );
   explicit basic_fstream( const std::string& filename,

                           std::ios_base::openmode mode               \fB(4)\fP \fI(since C++11)\fP

                               = std::ios_base::in |
   std::ios_base::out );
   template< class FsPath >

   explicit basic_fstream( const FsPath& filename,
                           std::ios_base::openmode mode               \fB(5)\fP \fI(since C++17)\fP

                               = std::ios_base::in |
   std::ios_base::out );
   basic_fstream( basic_fstream&& other );                            \fB(6)\fP \fI(since C++11)\fP
   basic_fstream( const basic_fstream& rhs ) = delete;                \fB(7)\fP \fI(since C++11)\fP

   Constructs new file stream.

   1) Default constructor: constructs a stream that is not associated with a file:
   default-constructs the std::basic_filebuf and constructs the base with the pointer
   to this default-constructed std::basic_filebuf member.
   2,3) First, performs the same steps as the default constructor, then associates the
   stream with a file by calling rdbuf()->open(filename, mode) (see
   std::basic_filebuf::open for the details on the effects of that call). If the open()
   call returns a null pointer, sets setstate(failbit).
   Overload \fB(3)\fP is only provided if std::filesystem::path::value_type is not char.
   \fI(since C++17)\fP
   4,5) Same as basic_fstream(filename.c_str(), mode).
   \fB(5)\fP participates in overload resolution only if FsPath is std::filesystem::path.
   \fI(since C++17)\fP
   6) Move constructor. First, move-constructs the base class from other (which does
   not affect the rdbuf() pointer), then move-constructs the std::basic_filebuf member,
   then calls this->set_rdbuf() to install the new basic_filebuf as the rdbuf() pointer
   in the base class.
   7) The copy-constructor is deleted: this class is not copyable.

.SH Parameters

   filename - the name of the file to be opened
              specifies stream open mode. Following constants and bit-wise OR between
              them may be used:

              Constant          Explanation
              app               seek to the end of stream before each write
   mode     - binary            open in binary mode
              in                open for reading
              out               open for writing
              trunc             discard the contents of the stream when opening
              ate               seek to the end of stream immediately after open
              noreplace (C++23) open in exclusive mode
   other    - another file stream to use as source

.SH Example


// Run this code

 #include <fstream>
 #include <string>
 #include <utility>

 int main()
 {
     std::fstream f0;
     std::fstream f1("test.bin", std::ios::binary);
     std::string name = "example.txt";
     std::fstream f2(name);
     std::fstream f3(std::move(f1));
 }

   Defect reports

   The following behavior-changing defect reports were applied retroactively to
   previously published C++ standards.

      DR    Applied to            Behavior as published              Correct behavior
                       the default argument of mode in overload
   LWG 460  C++98      (2)                                         added
                       was missing (it is present in the synopsis)
   LWG 3430 C++17      std::filesystem::path overload led to       avoided by making it
                       unwanted conversions                        a template

.SH See also

   open          opens a file and associates it with the stream
                 \fI(public member function)\fP
   open          opens a file and configures it as the associated character sequence
                 \fI(public member function of std::basic_filebuf<CharT,Traits>)\fP
   set_rdbuf     replaces the rdbuf without clearing its error state
                 \fI(protected member function)\fP
   constructor   constructs the object
                 \fI(public member function of std::basic_iostream<CharT,Traits>)\fP
